package com.beck.code.generator.generatorProcessor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.beck.code.generator.configProperty.CommonGeneratorConfig;
import com.beck.code.generator.configProperty.ContextConfig;
import com.beck.code.generator.configProperty.TableConfig;
import com.beck.code.generator.vo.JavaCodeVO;


/**
 * @author CuiJianbo
 * @since 2025/7/1
 */

public class DaoGenerator extends CommonGenerator {

    public DaoGenerator(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        super(tableConfig, introspectedTable, contextConfig, contextConfig.getDaoGeneratorConfig());
    }

    public static DaoGenerator getInstance(TableConfig tableConfig, IntrospectedTable introspectedTable, ContextConfig contextConfig) {
        return new DaoGenerator(tableConfig, introspectedTable, contextConfig);
    }

    @Override
    void generateCodeFile() {
        String daoPackage = contextConfig.getDaoGeneratorConfig().getTargetPackage();
        JavaCodeVO javaVO = new JavaCodeVO();
        BeanUtil.copyProperties(commonGeneratorVO, javaVO);
        javaVO.setPackageName(daoPackage);
        String data = applyTemplateResources(contextConfig.getDaoGeneratorConfig().getTemplate(), javaVO);
        //输出真实文件
        boolean flag = generateCodeFile(data);

        if (flag) {
            System.out.println(fileName());
        }
    }

    @Override
    String fileName() {
        String fileName = StrUtil.concat(true, StrUtil.upperFirst(entityName), "Dao.java");
        return contextConfig.getDaoGeneratorConfig().getTargetPackage().replace(".", "/") + "/" + fileName;
    }

}
